import 'package:equatable/equatable.dart';

/// 聊天消息模型
/// 使用Equatable实现相等性比较
class ChatMessageModel extends Equatable {
  /// 消息ID
  final String id;

  /// 群聊ID
  final String groupId;

  /// 发送者用户ID
  final String senderId;

  /// 消息内容
  final String content;

  /// 发送时间
  final DateTime sendTime;

  const ChatMessageModel({
    required this.id,
    required this.groupId,
    required this.senderId,
    required this.content,
    required this.sendTime,
  });

  /// 从JSON创建消息对象
  factory ChatMessageModel.fromJson(Map<String, dynamic> json) {
    return ChatMessageModel(
      id: json['id'] as String,
      groupId: json['groupId'] as String,
      senderId: json['senderId'] as String,
      content: json['content'] as String,
      sendTime: DateTime.parse(json['sendTime'] as String),
    );
  }

  /// 转换为JSON
  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'groupId': groupId,
      'senderId': senderId,
      'content': content,
      'sendTime': sendTime.toIso8601String(),
    };
  }

  /// 复制并修改部分属性
  ChatMessageModel copyWith({
    String? id,
    String? groupId,
    String? senderId,
    String? content,
    DateTime? sendTime,
  }) {
    return ChatMessageModel(
      id: id ?? this.id,
      groupId: groupId ?? this.groupId,
      senderId: senderId ?? this.senderId,
      content: content ?? this.content,
      sendTime: sendTime ?? this.sendTime,
    );
  }

  /// 验证消息内容是否有效
  bool get isValid {
    return content.trim().isNotEmpty;
  }

  @override
  List<Object?> get props => [id, groupId, senderId, content, sendTime];

  @override
  String toString() {
    return 'ChatMessageModel(id: $id, groupId: $groupId, senderId: $senderId, content: $content, sendTime: $sendTime)';
  }
}
